import tkinter as tk
from tkinter import ttk,  messagebox, Menu
from bilibili_danmu_analysis.gui.crawl_gui import CrawlFrame
from bilibili_danmu_analysis.gui.analyze_gui import AnalyzeFrame


class BilibiliGui:
    def __init__(self):
        self.root = tk.Tk()
        self._setup_main_window()
        self.data = []
        self._create_menus()
        self.show_welcome_screen()

    def _setup_main_window(self):
        self.root.title("B站弹幕分析工具")
        self.root.geometry("800x600")
        self.root.minsize(800, 600)
        self.root.configure(background="#F0F0F0")

    def _create_menus(self):
        menubar = Menu(self.root)
        self.root.config(menu=menubar)

        # 功能菜单
        func_menu = Menu(menubar, tearoff=0)
        func_menu.add_command(label="数据采集", command=self.show_crawl_interface)
        func_menu.add_command(label="数据分析", command=self.show_analyze_interface)
        menubar.add_cascade(label="功能", menu=func_menu)

        # 帮助菜单
        help_menu = Menu(menubar, tearoff=0)
        help_menu.add_command(label="关于", command=self._show_about)
        menubar.add_cascade(label="帮助", menu=help_menu)

    def show_welcome_screen(self):
        self._clear_current_frame()
        self.current_frame = ttk.Frame(self.root)
        self.current_frame.pack(expand=True, fill="both")

        welcome_label = tk.Label(
            self.current_frame,
            text="欢迎使用 B站弹幕分析工具\n\n版本 1.0",
            font=("Arial", 16),
            foreground="white",
            background="#66acc2",
            width=400,
            height=20,
            anchor="center",
            relief="solid",
            borderwidth=2
        )
        welcome_label.pack(pady=150)

    def show_crawl_interface(self):
        self._clear_current_frame()
        self.current_frame = CrawlFrame(
            self.root,
            self.data
        )
        self.current_frame.pack(expand=True, fill="both")

    def show_analyze_interface(self):
        self._clear_current_frame()
        self.current_frame = AnalyzeFrame(
            self.root,
            self.data
        )
        self.current_frame.pack(expand=True, fill="both")

    def _clear_current_frame(self):
        if hasattr(self, 'current_frame'):
            self.current_frame.destroy()

    def _show_about(self):
        about_info = """B站弹幕分析工具 v1.0
        开发者：我是小丑
        联系方式：没有联系方式"""
        messagebox.showinfo("关于", about_info)

    def run(self):
        self.root.mainloop()


if __name__ == '__main__':
    app = BilibiliGui()
    app.run()
